package net.csdra.DongPoShuZiTuShuGuan.service;

import net.csdra.DongPoShuZiTuShuGuan.domain.ExternalLink;
import net.csdra.DongPoShuZiTuShuGuan.domain.ExternalLinkRepository;
import net.csdra.DongPoShuZiTuShuGuan.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * Created by add on 2018/1/17.
 */
@Service
public class ExternalLinkManagerServiceImpl implements ExternalLinkManagerService {
    @Autowired
    ExternalLinkRepository externalLinkRepository;

    @Override
    public ExternalLink getExternalLinkInfo(GetExternalLinkInfo getExternalLinkInfo) {
        return this.externalLinkRepository.findOne(getExternalLinkInfo.getId());
    }

    @Override
    public ExternalLink upadteExternalLink(UpadteExternalLink upadteExternalLink) {
        Assert.notNull(upadteExternalLink, "参数不能为空");
        Assert.hasText(upadteExternalLink.getId(), "id不能为空");
        Assert.hasText(upadteExternalLink.getResourcesName(),"@名称不能为空");
        ExternalLink externalLink = this.externalLinkRepository.findOne(upadteExternalLink.getId());
        Assert.notNull(externalLink, "修改对象不存在");
        Utils.copy(upadteExternalLink, externalLink);
        this.externalLinkRepository.save(externalLink);
        ExternalLink externalLink1 = new ExternalLink();
        Utils.copy(externalLink, externalLink1);
        return externalLink1;

    }

    @Override
    public void deleteExternalLink(DeleteExternalLink deleteExternalLink) {
        Assert.notNull(deleteExternalLink, "参数不能为空");
        Assert.hasLength(deleteExternalLink.getExternalLinkId(), "ID不能为空");
        ExternalLink externalLink = this.externalLinkRepository.findOne(deleteExternalLink.getExternalLinkId());
        Assert.notNull(externalLink, "对象不存在");
        externalLink.setDeleted(true);
        this.externalLinkRepository.save(externalLink);
    }

    @Override
    public ExternalLink createExternalLink(CreateExternalLink createExternalLink) {
        Assert.notNull(createExternalLink, "参数不能为空");
        Assert.hasText(createExternalLink.getResourcesName(),"@名称不能为空");
        Assert.hasText(createExternalLink.getMetaId(), "MetaId不能为空");
        Long l = this.externalLinkRepository.countByMetaId(createExternalLink.getMetaId());

        ExternalLink externalLink = Utils.createAutoSet(ExternalLink.class, "system");
        Utils.copy(createExternalLink, externalLink);
        externalLink.setModifyOrder(true);
//        this.checkOrder(createExternalLink.getMetaId(),createExternalLink.getViewOrder(),externalLink);
        this.externalLinkRepository.save(externalLink);
        ExternalLink externalLink1 = new ExternalLink();
        Utils.copy(externalLink, externalLink1);
        return externalLink1;
    }

    @Override
    public Page<ExternalLink> getExternalLinks(GetExternalLinks getExternalLinks) {
        Assert.notNull(getExternalLinks, "参数不能为空");
        Assert.hasText(getExternalLinks.getMetaId(), "metaId不能为空");
        Page<ExternalLink> page = this.externalLinkRepository.findByMetaId(getExternalLinks.getMetaId(),
                new PageRequest(getExternalLinks.getPage(), getExternalLinks.getSize(), Sort.Direction.ASC, "viewOrder"));
        return page;
    }




}
